//
//  YLForgetPasswordViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/17.
//  Copyright © 2017年 she1323. All rights reserved.
//  忘记密码

#import "YLForgetPasswordViewController.h"
#import "YLGlobeHeader.h"
#import "YLForgetPasswordViewModel.h"
#import "YLVerificationCodeViewModel.h"
#import "YLNewPasswordViewController.h"
#import "WHProgressHUD.h"
#import "WHNetworkTool.h"
@interface YLForgetPasswordViewController ()


/**
 手机号
 */
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;


/**
 短信验证码
 */
@property (weak, nonatomic) IBOutlet UITextField *codeWithMessageTextField;

@property (nonatomic, strong) YLForgetPasswordViewModel *fpViewModel;
@property (nonatomic, strong) YLVerificationCodeViewModel *vcViewModel;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *verificationButton;
@property (weak, nonatomic) IBOutlet UIView *phoneNumberLine;

@property (weak, nonatomic) IBOutlet UIView *codeLine;

@end

@implementation YLForgetPasswordViewController

- (YLForgetPasswordViewModel *)fpViewModel
{
    if (!_fpViewModel) {
        _fpViewModel = [[YLForgetPasswordViewModel alloc] init];
    }
    return _fpViewModel;
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
   // [[self.navigationController.navigationBar.subviews objectAtIndex:0]setAlpha:0];

}

- (YLVerificationCodeViewModel *)vcViewModel
{
    if (!_vcViewModel) {
        _vcViewModel = [[YLVerificationCodeViewModel alloc] init];
        _vcViewModel.urlStr = @"Account/PGetModifyPassWdVerifyCode";
    }
    return _vcViewModel;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setUpUI];
    
    [self bindViewModel];
    
    [self didClickEvent];
}

- (void)didClickEvent
{

    __weak typeof(self) weakSelf = self;
    
    [[_loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        YLNewPasswordViewController *newPassword = [[YLNewPasswordViewController alloc] init];
        newPassword.verifyCode = weakSelf.codeWithMessageTextField.text;
        newPassword.telCode = weakSelf.phoneNumberTextField.text;
        [weakSelf.navigationController pushViewController:newPassword animated:YES];
        
    }];
    
    
    [[_verificationButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf.vcViewModel.verificationCommand execute:weakSelf.phoneNumberTextField.text];
        
    }];
    
    [self.vcViewModel.verificationCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        [weakSelf.verificationButton setTitle:x forState:UIControlStateNormal];
    }];
    
    
    [[self.vcViewModel.verificationCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            weakSelf.verificationButton.userInteractionEnabled = NO;
        } else {
            weakSelf.verificationButton.userInteractionEnabled = YES;
        };
    }];
}

- (void)setUpUI
{
    self.title = @"重置密码";
    
    _verificationButton.layer.borderColor = YLMainColor.CGColor;
    __weak typeof(self) weakSelf = self;
    
    _phoneNumberLine.backgroundColor = _codeLine.backgroundColor = YLLineColor;
    
    [[_phoneNumberTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.phoneNumberLine.backgroundColor = YLMainColor;
        weakSelf.codeLine.backgroundColor = YLLineColor;
    }];
    
    
    [[_codeWithMessageTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.codeLine.backgroundColor = YLMainColor;
        weakSelf.phoneNumberLine.backgroundColor = YLLineColor;
    }];
    
    [_phoneNumberTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 11) {
            weakSelf.phoneNumberTextField.text = [weakSelf.phoneNumberTextField.text substringToIndex:11];
        }
    }];
    
    [_codeWithMessageTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 6) {
            weakSelf.codeWithMessageTextField.text = [weakSelf.codeWithMessageTextField.text substringToIndex:6];
        }
    }];
}

- (void)bindViewModel
{
    RAC(self.fpViewModel, phoneNumber) = _phoneNumberTextField.rac_textSignal;
    RAC(self.fpViewModel, codeWithMessage) = _codeWithMessageTextField.rac_textSignal;
    RAC(self.loginButton, enabled) = self.fpViewModel.loginEnableSignal;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
